package com.cqw.commontest.mongodb.service;

import com.cqw.commontest.mongodb.dao.UserRepository;
import com.cqw.commontest.mongodb.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-08-04 23:55
 * @Description:
 */
@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    @Resource
    private MongoTemplate mongoTemplate;


    /**
     * 保存
     *
     * @param user
     */
    public void save(User user) {
        userRepository.save(user);
    }


    /**
     * 更新
     *
     * @param user
     */
    public void update(User user) {
        userRepository.save(user);
    }


    /**
     * 根据id删除
     *
     * @param id
     */
    public void deleteById(String id) {
        userRepository.deleteById(id);
    }


    /**
     * 查询所有
     *
     * @return
     */
    public List<User> findAll() {
        return userRepository.findAll();
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    public User findById(String id) {
        return userRepository.findById(id).orElse(null);
    }


    /**
     * 根据state和sex查询
     *
     * @param state
     * @param sex
     * @return
     */
    public List<User> findByStateAndSex(Integer state, Integer sex) {
        return userRepository.findByStateAndSex(state, sex);
    }


    /**
     * 将关注量+1
     *
     * @param id
     */
    public void incFollowCount(String id) {
        //构造查询对象
        Query query = Query.query(Criteria.where("_id").is(id));
        //构造更新对象
        Update update = new Update();
        update.inc("followNum");
        //执行
        mongoTemplate.updateFirst(query, update, User.class);
    }


    /**
     * 根据年龄分页查询
     *
     * @param age
     * @return
     */
    public Page<User> findByAge(Integer age, Integer pageNum, Integer pageSize) {
        //构造分页条件
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        return userRepository.findByAge(age, pageRequest);
    }


    /**
     * template 分页查询
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    public List<User> findPageByTemplate(User user, Integer pageNum, Integer pageSize) {
        //构造查询对象
        Query query = new Query();
        //设置参数
        if (StringUtils.isNotBlank(user.getName())) {
            query.addCriteria(Criteria.where("name").regex(user.getName() + ".*"));
        }

        if (user.getAge() != null) {
            query.addCriteria(Criteria.where("age").lt(user.getAge()));
        }

        if (user.getSex() != null) {
            query.addCriteria(Criteria.where("sex").is(user.getSex()));
        }

        //跳过多少条
        query.skip((long) (pageNum - 1) * pageSize);
        //取出多少条
        query.limit(pageSize);

        //构造排序对象
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "age");
        //设置排序对象
        query.with(Sort.by(order));

        return mongoTemplate.find(query, User.class);
    }


    public List<User> findBy() {
        Query query = new Query(new Criteria().andOperator(Criteria.where("id").is(2)).and("isUse").is(true));
        // query.addCriteria(Criteria.where("userNameList.id").is(2).and("userNameList.isUse").is(true));


        List<User> users = mongoTemplate.find(query, User.class,"user");
        return users;
    }

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        List<String> collect = strings.stream().map(s -> {
            if ("1".equals(s)) {
                return s;
            }
            return null;
        }).collect(Collectors.toList());
        System.out.println(collect);
    }




    //======================================================================
    //public User


























}
